package com.example.petadopt.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.example.petadopt.common.Result;
import com.example.petadopt.entity.User;
import com.example.petadopt.utils.SqliteUtils;

/**
 * 用户数据访问对象
 */
@SuppressLint("Range")
public class UserDao {

    /**
     * 用户注册
     */
    public static Result<Void> register(User user) {
        if (user == null || TextUtils.isEmpty(user.getUsername()) || TextUtils.isEmpty(user.getPassword())) {
            return new Result<>(false, "用户名或密码不能为空", null);
        }

        Result<User> getByUsernameResult = getByUsername(user.getUsername());
        if (getByUsernameResult.isSuccess()) {
            return new Result<>(false, "用户名已存在", null);
        }

        ContentValues values = new ContentValues();
        values.put("username", user.getUsername());
        values.put("password", user.getPassword());
        long i = SqliteUtils.getInstance().getWritableDatabase().insert("tb_user", null, values);
        if (i > 0) {
            return new Result<>(true, "注册成功", null);
        }
        return new Result<>(false, "注册失败", null);
    }

    /**
     * 用户登录
     */
    public static Result<User> login(String username, String password) {
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            return new Result<>(false, "用户名或密码不能为空", null);
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_user", null, "username=? and password=?", new String[]{username, password}, null, null, null);
        if (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getLong(cursor.getColumnIndex("id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
            cursor.close();
            return new Result<>(true, "登录成功", user);
        }
        cursor.close();
        return new Result<>(false, "用户名或密码错误", null);
    }

    /**
     * 根据用户名查询用户
     */
    public static Result<User> getByUsername(String username) {
        if (TextUtils.isEmpty(username)) {
            return new Result<>(false, "用户名不能为空", null);
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_user", null, "username=?", new String[]{username}, null, null, null);
        if (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getLong(cursor.getColumnIndex("id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
            cursor.close();
            return new Result<>(true, "查询成功", user);
        }
        cursor.close();
        return new Result<>(false, "用户不存在", null);
    }

    /**
     * 根据用户ID查询用户
     */
    public static Result<User> getByUserId(Long userId) {
        if (userId == null) {
            return new Result<>(false, "用户ID不能为空", null);
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_user", null, "id=?", new String[]{String.valueOf(userId)}, null, null, null);
        if (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getLong(cursor.getColumnIndex("id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
            cursor.close();
            return new Result<>(true, "查询成功", user);
        }
        cursor.close();
        return new Result<>(false, "用户不存在", null);
    }
}
